<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / Adchannel.php    [ 2024/12/26 13:55 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\service\finder;

use zhanshop\App;
use zhanshop\Helper;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Adchannel extends BaseFinder
{
    protected $tabs = [
        [
            'title' => '全部应用',
            'where' => [
                'app',
                '=',
                ''
            ],
        ],
    ];

    protected $headToolbar = [];

    protected $rowToolbar = [
        [
            'event' => 'edit',
            'ico' => '&#xe642;',
            'title' => '编辑',
            'method' => '',
            'page' => './table/edit.html',
        ],
    ];

    public function __construct(string $menuId)
    {
        parent::__construct($menuId);
        $apps = App::database()->model("ad_app")->order('create_time asc')->limit(20)->select();
        foreach ($apps as $app){
            $this->tabs[] = [
                'title' => $app['name'],
                'where' => [
                    'app',
                    '=',
                    $app['app']
                ],
            ];
        }
    }

    public function searchfrom(Request $request, Response $response){
        $data = parent::searchfrom($request, $response);
        $tabs = $this->tabs;
        unset($tabs[0]);
        $apps = [];
        foreach ($tabs as $tab){
            $app = $tab['where'][2];
            $apps[$app] = $tab['title'];
        }
        $data['app']['value'] = $apps;
        return $data;
    }

    /**
     * 编辑
     * @param Request $request
     * @param Response $response
     * @return array
     */
    public function editfrom(Request $request, Response $response){
        $data = parent::editfrom($request, $response);
        $domain = $request->header('origin');
        if($domain == false) $domain = 'https://'.$request->header('host');
        switch ($data[0]['schema']['name']['default']){
            case "baidu":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/baidu/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D?aid=__IDEA_ID__&pid=__PLAN_ID__&uid=__UNIT_ID__&userid=__USER_ID__&click_id=__CLICK_ID__&idfa=__IDFA__&imei=__IMEI__&android_id=__ANDROIDID__&ip=__IP__&ua=__UA__&os=__OS__&ts=__TS__&ext_info=__EXT_INFO__&mac_md5=__MAC1__&mac=__MAC__&oaid=__OAID__&oaid_md5=__OAID_MD5__&comb_id=__COMBID__&size=__SIZE__&deeplink_url=__DEEPLINK_URL__&bd_vid=__BD_VID__&ext_info=__EXT_INFO__&callType=v2';
                $data[0]['schema']['click_listen']['title'] = '点击监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'click', $url);
                break;
            case "byte":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/byte/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D?promotion_id=__PROMOTION_ID__&project_id=__PROJECT_ID__&promotion_name=__PROMOTION_NAME__&project_name=__PROJECT_NAME__&mid1=__MID1__&mid2=__MID2__&mid3=__MID3__&mid4=__MID4__&mid5=__MID5__&mid6=__MID6__&aid=__AID__&aid_name=__AID_NAME__&cid=__CID__&cid_name=__CID_NAME__&campaign_id=__CAMPAIGN_ID__&campaign_name=__CAMPAIGN_NAME__&ctype=__CTYPE__&advertiser_id=__ADVERTISER_ID__&csite=__CSITE__&convert_id=__CONVERT_ID__&request_id=__REQUEST_ID__&track_id=__TRACK_ID__&sl=__SL__&imei=__IMEI__&idfa=__IDFA__&idfa_md5=__IDFA_MD5__&android_id=__ANDROIDID__&oaid=__OAID__&oaid_md5=__OAID_MD5__&os=__OS__&mac=__MAC__&mac1=__MAC1__&ipv4=__IPV4__&ipv6=__IPV6__&ip=__IP__&ua=__UA__&geo=__GEO__&ts=__TS__&callback_param=__CALLBACK_PARAM__&callback_url=__CALLBACK_URL__&model=__MODEL__&union_site=__UNION_SITE__&caid=__CAID__&caid_md5=__CAID_MD5__&productid=__PRODUCTID&outerid=__OUTERID__';
                $data[0]['schema']['show_listen']['title'] = "点击广告监测";
                $data[0]['schema']['show_listen']['default'] = str_replace('{{EVENT}}', 'show', $url);
                $data[0]['schema']['click_listen']['title'] = '播放监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'click', $url);
                $data[0]['schema']['down_listen']['title'] = '播放结束监测';
                $data[0]['schema']['down_listen']['default'] = str_replace('{{EVENT}}', 'pay', $url);
                $data[0]['schema']['install_listen']['title'] = '播放有效监测';
                $data[0]['schema']['install_listen']['default'] = str_replace('{{EVENT}}', 'payend', $url);
                break;
            case "xiaomi":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/xiaomi/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D?response_validate=false&imei=__IMEI__&imei2=__IMEI2__&meid=__MEID__&oaid=__OAID__&click_time=__TS__&app_id=__APPID__&adid=__ADID__&campaign_id=__CAMPAIGNID__&customer_id=__CUSTOMERID__&callback=__CALLBACK__&sign=__SIGN__&expId=__EXPID__&ip=__IP__&ua=__UA__&androidId=__ANDROIDID__&adName=__ADNAME__&rtaExpId=__RTAEXPID__';
                $data[0]['schema']['click_listen']['title'] = '点击监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'click', $url);
                break;
            case "vivo":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/vivo/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D';
                $data[0]['schema']['click_listen']['title'] = '点击监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'show', $url);
                break;
            case "oppo":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/oppo/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D?pkg_name='.$data[0]['schema']['pkg_name']['default'].'&ad=__ADID__&imei=__IMEI__&oaid=__OAID__&ts=__TS__&android_id=__ANDROIDID__&ip=__IP__&ua=$ua$&owner_id=$ownerid$&plan_id=$planid$&group_id=$groupid$';
                $data[0]['schema']['click_listen']['title'] = '点击监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'click', $url);
                break;
            case "huawei":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/huawei/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D?aid=__AID__&aid_name=__AID_NAME__&app_id=__APP_ID__&app_name=__APP_NAME__&channel_name=__CHANNEL_NAME__&channel_id=__CHANNEL_ID__&group_name=__GROUP_NAME__&group_id=__GROUP_ID__&oaid=__OAID__&id_type=__ID_TYPE__&unique_id=__UNIQUE_ID__&action_type=__ACTION_TYPE__&ts=__TS__&callback=__CALLBACK__&sub_taskid=__SUB_TASKID__&rtaid=__RTAID__';
                $data[0]['schema']['show_listen']['title'] = '展示监测';
                $data[0]['schema']['show_listen']['default'] = str_replace('{{EVENT}}', 'show', $url);
                $data[0]['schema']['click_listen']['title'] = '点击监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'click', $url);
                $data[0]['schema']['down_listen']['title'] = '下载监测';
                $data[0]['schema']['down_listen']['default'] = str_replace('{{EVENT}}', 'pay', $url);
                $data[0]['schema']['install_listen']['title'] = '安装监测';
                $data[0]['schema']['install_listen']['default'] = str_replace('{{EVENT}}', 'payend', $url);
                break;
            case "honor":
                $url = $domain.'/v1/ad.listen/'.$data[0]['schema']['app']['default'].'/honor/{{EVENT}}/CEgm1pj2JXNmnlEI1gn2JdHryzcsHbxq1huin_2BlBvDM_3D?os=__OS__&ip=__IP__&oaid=__OAID__&imei=__IMEI__&requestid=__REQUESTID__&advertiser_id=__ADVERTISER_ID__&track_id=__TRACK_ID__&creative_id=__CREATIVE_ID__&time=__TIME__&ua=__UA__&groupid=__GROUPID__&campaignid=__CAMPAIGNID__';
                $data[0]['schema']['show_listen']['title'] = '展示监测';
                $data[0]['schema']['show_listen']['default'] = str_replace('{{EVENT}}', 'show', $url);
                $data[0]['schema']['click_listen']['title'] = '点击监测';
                $data[0]['schema']['click_listen']['default'] = str_replace('{{EVENT}}', 'click', $url);
                $data[0]['schema']['down_listen']['title'] = '下载监测';
                $data[0]['schema']['down_listen']['default'] = str_replace('{{EVENT}}', 'down', $url);
                $data[0]['schema']['install_listen']['title'] = '安装监测';
                $data[0]['schema']['install_listen']['default'] = str_replace('{{EVENT}}', 'install', $url);
                break;
        }
        return $data;
    }

    public function put(Request $request, Response $response){
        $input = $request->validateData($request->get(), [
            'pk' => 'Required',
        ])->getData();
        $post = $request->post();
        App::database()->model($this->menuData['table_names'][0])->transaction(function(mixed $pdo) use ($post, $input){
            $pk = 0;
            foreach ($post as $k => $v){
                $table = $this->menuData['table_names'][$k] ?? App::error()->setError($k.'没有对应的table', 403);
                $include = include Helper::getSchemaPath($table);
                if(isset($include['update_time'])) $include['update_time'] = time();
                foreach($v as $kk => $vv){
                    if(is_array($vv)) $v[$kk] = json_encode($vv, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
                    if(strpos($kk, '_listen') !== false){
                        unset($v[$kk]);
                    }
                }
                $getId = App::database()->model($table)->where([$this->menuData['pk'] => $input['pk']])->update($v, $pdo);
            }
        });
        // 再触发一下查询接口
        return $this->data(1, 1, [[$this->menuData['pk'], '=', $input['pk']]]);
    }
}